
#define SDL_VIDEO_OPENGL_EGL

#include "sdl2/SDL.h"
#include <string>

#include "stringtool/stringconv.h"
#include "gles.h"

#include"sdl2/SDL_opengl.h"
#include "GLES/egl.h"
#ifdef CMAKEWIN
#include <Windows.h>


int _realmain();


int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
	_In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{


	return _realmain();
}
#else
int _realmain();


#endif


int _realmain()
{
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);

	SDL_Init(SDL_INIT_VIDEO);
	//int r = SDL_GL_LoadLibrary("libegl.dll");
	//HMODULE module = LoadLibraryA("libglesv2.dll");
	
	//const char* errstr= SDL_GetError();
	//printf("err %s", errstr);
	std::string strTitle = StringConv::ToUtf8String(u"cool title.");
	Uint32 sdl_flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
	sdl_flags |= SDL_WINDOW_ALLOW_HIGHDPI;
	//sdl_flags |= SDL_WINDOW_OPENGL;
	SDL_Window* winMain = SDL_CreateWindow(strTitle.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 800, sdl_flags);
	SDL_GL_CreateContext(winMain);

	//PFNGLCLEARCOLORXPROC glClearColor = (PFNGLCLEARCOLORXPROC)(void*)GetProcAddress(module, "glClearColor");
	//PFNGLCLEARPROC glClear = (PFNGLCLEARPROC)(void*)GetProcAddress(module, "glClear");
	//PFNGLVIEWPORTPROC glViewport = (PFNGLVIEWPORTPROC)GetProcAddress(module, "glViewport");
	//PFNGLCLEARCOLORXPROC glClearColor = (PFNGLCLEARCOLORXPROC)SDL_GL_GetProcAddress("glClearColor");
	//PFNGLCLEARPROC glClear = (PFNGLCLEARPROC)SDL_GL_GetProcAddress("glClear");
	//PFNGLVIEWPORTPROC glViewport =(PFNGLVIEWPORTPROC)SDL_GL_GetProcAddress("glViewport");
	GL_Init(winMain);
	SDL_Event event;
	bool bExit = false;
	while (!bExit)
	{
		while (SDL_PollEvent(&event))
		{
			if (event.type == SDL_QUIT)
			{
				bExit = true;
			}

		}

		{
			GL_Loop();
		
		}
	}
	SDL_DestroyWindow(winMain);
	SDL_Quit();
	return 0;
}

